'use strict';

const Controller = require('egg').Controller;

class ArticleController extends Controller {
  async list() { // 文章列表
    const checkResult = this.ctx.helper.checkLogin(this);
    let params = this.ctx.request.body;
    params.params = this.ctx.helper.filterObj(params.params);
    params.pageSize = Number(params.pageSize);
    params.current = Number(params.current);
    const res = await this.ctx.service.article.list(params, checkResult.result);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async category() { // 分类列表
    const res = await this.ctx.service.article.category();
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async save() { // 保存文章
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.article.save(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async detail() { // 文章详情
    const checkResult = this.ctx.helper.checkLogin(this);
    const res = await this.ctx.service.article.detail(this.ctx.request.body, checkResult.result);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async sendComment() { // 评论
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.article.sendComment(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async commentList() { // 评论列表
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.article.commentList(this.ctx.request.body, checkResult.result);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async likeComment() { // 给评论点赞
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.article.likeComment(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }

  async likeArticle() { // 给文章点赞
    const checkResult = this.ctx.helper.checkLogin(this);
    if (!checkResult.result) {
      this.ctx.body = checkResult.body;
      this.ctx.status = checkResult.body.status;
      return false;
    }
    const res = await this.ctx.service.article.likeArticle(this.ctx.request.body);
    this.ctx.body = res;
    this.ctx.status = res.status;
  }
}

module.exports = ArticleController;
